const path = require('path')
//const glob = require('globby') //https://www.cnblogs.com/waitforyou/p/7044171.html
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')

const merge = require('webpack-merge')
const baseWebpackConfig = require('./webpack.base.conf.js')


// CSS入口配置
// var CSS_PATH = {
//     css: {
//         pattern: ['./src/**/[^_]*.less', '!./src/old/**/*.less'],
//         src: path.join(__dirname, 'src'),
//         dst: path.resolve(__dirname, 'static/build/webpack'),
//     }
// }

// 遍历出所有需要打包的CSS文件路径
// function getCSSEntries(config) {
//     var fileList = glob.sync(config.pattern)
//     return fileList.reduce(function (previous, current) {
//         var filePath = path.parse(path.relative(config.src, current))
//         var withoutSuffix = path.join(filePath.dir, filePath.name)
//         previous[withoutSuffix] = path.resolve(__dirname, current)
//         return previous
//     }, {})
// }


const webpackConfig = merge(baseWebpackConfig, {
    // entry: getCSSEntries(CSS_PATH.css),
    entry: {},
    output: {
        filename: 'js/[name].[hash:7].js',
        path: path.resolve()+'/dist/',
        //publicPath: "./"
    },
    module: {
        rules: [
            {
                test: /\.(css|scss|sass)$/,
                use: ExtractTextPlugin.extract({        //此处为分离css的写法
                    fallback: 'style-loader',
                    //resolve-url-loader may be chained before sass-loader if necessary
                    use: ['css-loader', 'postcss-loader', 'sass-loader']
                })
            }
        ]
    },
    plugins: [
        // 清除 dist 文件夹内的文件
        new CleanWebpackPlugin('/dist', {
            root: path.resolve(),
            verbose: true,
            dry: true
        }),

        // 将css打包成文件
        new ExtractTextPlugin({
            filename: './css/[name].css',
            //publicPath: ''
        }),

        new CopyWebpackPlugin([
            {
                from: './src/assets',
                to: './assets',
                toType: 'dir'
            }
        ])


    ],
    // 模式配置
    mode: 'production'
})

module.exports = webpackConfig